package bing.commons.xmloperator;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.google.common.io.Files;

/*
 * 读取.classpath文件，解析path字段，把path字段的jar拷贝出来，
 */
public class ReadXmlAndCopyFile {

	private static List<String> classpathfilelist=new ArrayList();
	private static List<String> pathvarlist=new ArrayList();
	private static final String destpath="D:/workspace/atlasdb-build";
	public static void main(String[] args) throws DocumentException, IOException {
		// TODO Auto-generated method stub
		//step1 找出所有.classpath 文件，并存在数组classpathfilelist中
		findclasspathFile("D:/projects/javaprojects/atlasdb-0.3.28");
		
		//step2 找出所有的.classpath 文件中的path变量，并存在数组pathvarlist中
		for(int i=0;i<classpathfilelist.size();i++){
			parseAllXml(classpathfilelist.get(i));
		}
		
		//step3 根据pathvarlist数组，拷贝文件
		copyJarFile(pathvarlist,destpath);
	}

	/*
	 * 找出所有的classpath文件，并记录在List<String> classpathfilelist变量中。
	 */
	public static void findclasspathFile(String fileName){
        File file = new File(fileName);  
        if (file.isDirectory()) {  
            File[] files = file.listFiles();  
            for (int i = 0; i < files.length; i++) {  
                String name = files[i].getName();  
                if (name.trim().toLowerCase().endsWith(".classpath")) {  
                    System.out.println(files[i].getPath() + "\t");  
                    classpathfilelist.add(files[i].getPath());//添加到list中
                }  
                if (files[i].isDirectory()) {  
                    String path = files[i].getPath();  
                    findclasspathFile(path);  
                }  
            }  
        }  
	}
	public static void parseAllXml(String filepath) throws DocumentException{
		SAXReader reader = new SAXReader();
		  Document document = reader.read(new File(filepath));
		  Element root = document.getRootElement();

		  Iterator it = root.elementIterator();
		  while (it.hasNext()) {
		   Element element = (Element) it.next();

		   //未知属性名称情况下
		   /*Iterator attrIt = element.attributeIterator();
		   while (attrIt.hasNext()) {
		    Attribute a  = (Attribute) attrIt.next();
		    System.out.println(a.getValue());
		   }*/

		   //已知属性名称情况下
		   if(element.attributeValue("path").endsWith(".jar")){
		   System.out.println("path: " + element.attributeValue("path"));
		   pathvarlist.add(element.attributeValue("path"));
		   }
		   //未知元素名情况下
		   /*Iterator eleIt = element.elementIterator();
		   while (eleIt.hasNext()) {
		    Element e = (Element) eleIt.next();
		    System.out.println(e.getName() + ": " + e.getText());
		   }
		   System.out.println();*/

		   //已知元素名情况下
//		   System.out.println("title: " + element.elementText("title"));
//		   System.out.println("author: " + element.elementText("author"));
//		   System.out.println();
		  }
	}
	
	public static void copyJarFile(List<String> sourcepathList,String destpath) throws IOException{
	       //复制移动文件  
		int listsize=sourcepathList.size();
		for(int i=0;i<listsize;i++){
	        File from=new File(sourcepathList.get(i));  
	        File to=new File(destpath+"/"+from.getName());  
	        Files.copy(from, to);  
		}

        //Files.move(from, to
	}
 
}
